Text box (text input)
Allows users to enter a single line of text.
Examples
Usage
Use a text box when the user needs to enter a single line of text, such as a name or email address. Every text box must have a label. If the field is required, show an asterisk (*) next to the label.
You can include optional guidance text to help users understand what to enter. If the guidance is longer than a short sentence, use a guidance accordion instead.
Choose the appropriate input type in the HTML to help with accessibility, validation and the right on-screen keyboard on touch-screen devices.
Common input types
- text – for general short text
- number - for numbers
- email – for email addresses
- tel – for phone numbers
- password – for concealed input
Do:
- use a text box only for single-line input, for longer text, use a text area
- choose a text box size that matches the length of content users are expected to enter
- always include a visible label that clearly describes what the user should enter
Avoid:
- using a text box when users must choose from a set list of options, instead use a select or radio button group
- relying on placeholder text because it can be inaccessible to some users, may not meet contrast requirements, and can be mistaken for pre-filled content
Parameters for turas-text-box
- asp-for - binds the text box to a model property
- guidance-text - provides additional guidance for the user
- field-column-widths - sets the column width for the text box
- additional-classes - allows for custom CSS classes to be added
- hide-validation-message - suppresses validation messages for the field
- readonly - makes the text box read-only
- asp-is-disabled - disables the text box
- hide-label - hides the label associated with the text box
Are you sure you want to close without saving?
Any information you have entered will be lost!
Testing
Check that the text box:
- can be used with a keyboard only, with no keyboard traps
- allows users to type, edit and delete text using only the keyboard
- shows a visible focus state when the text box receives focus
- is announced correctly by screen readers, including its label, role and current value
- clearly communicates any format requirements before users enter information
- hint text or instructions are read out (for example, character limits or formatting guidance) by screen readers
- supports browser autocomplete where appropriate
- error messages are announced by screen readers and associated with the text box
- input does not trigger unexpected changes while typing
- required fields are clearly communicated to all users
- character limits (if present) are communicated clearly and updated as users type
- remains usable at 400% zoom and supports reflow without horizontal scrolling
Developer considerations
When using the text box component in your form:
- provide a visible label using <label for=""> and an ID
- use the appropriate HTML input type where available (for example, email, tel or search)
- use the appropriate autocomplete attribute where recognised values are expected
- provide format requirements before the user enters information
- make sure the text box has a programmatic name (via label or aria-labelledby)
- use aria-describedby to associate hint text and error messages with the text box
- write error messages that explain what went wrong and how to fix it
- do not rely on placeholder text as a label
- make sure a visible focus state that meets contrast requirements
- make sure the text box remains usable when text is resized and content reflows
- avoid auto-submitting or triggering unexpected changes while typing
- make sure sufficient touch target size and spacing, with a target size of at least 24 × 24 CSS pixels
- if a character limit is used, communicate it clearly
- update the character count correctly as users type
- provide feedback when users approach or exceed the limit
- make sure error states are clearly visible and are programmatically associated with the text box
Required fields
If your form contains fields that users must complete:
- clearly indicate when the text box is required by using a visual indicator such as an asterisk
- do not rely on the visual indicator alone (for example, an asterisk) to communicate that a field is required, include the help text “Required fields are marked with an asterisk *” at the top of every page of the form
- make sure the required state of the text box is programmatically available to assistive technologies using aria-required="true